﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.Text;
using MS;
using System.IO;
namespace TotalSport
{
    public partial class ViewAllCustomer : System.Web.UI.Page
    {
        string strCon = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
        public SqlConnection conn;
        
        protected void Page_Load(object sender, EventArgs e)
        {
          // GridviewDataBind();


            if (Request.UrlReferrer == null)
            {
                Response.Redirect("Login.aspx");
            }
            if (!IsPostBack)
            {
                if (Request.UrlReferrer == null)
                {
                    Response.Redirect("Login.aspx");
                }

                GridviewDataBind();
            }
            GridviewDataBind();
        }

        public DataSet GetAllCustomer()
        {
            //  
            try
            {
                conn = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand("TS_GetAllCustomers", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                SqlDataAdapter sdptr = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sdptr.Fill(ds);
                conn.Close();
                return ds;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }

        protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridviewDataBind();
        }


        protected void lnkDownloadXcel_Click(object sender, EventArgs e)
        {

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}",DateTime.Now + ".xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
           // GridView1.DataBind();
           
            GridView1.AllowPaging = false;
           // GridView1.DataSource = GetAllCustomer();
            GridView1.DataBind();
            //Change the Header Row back to white color
            GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            {
                GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            }
            int j = 1;
            //This loop is used to apply stlye to cells based on particular row
            foreach (GridViewRow gvrow in GridView1.Rows)
            {
                gvrow.BackColor = Color.White;
                if (j <= GridView1.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                        }
                    }
                }
                j++;
            }
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

        }
        public void GridviewDataBind()
        {
            DataSet ds = GetAllCustomer();
            if (ds.Tables.Count > 0)
            {

                lnkDownloadXcel.Visible = true;

                if (ds.Tables[0].Rows.Count == 0)
                {
                    lnkDownloadXcel.Visible = false;
                }
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }


        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    }
}